<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;

use app\common\logic\LogicBase;
use app\api\error\CodeBase;
use app\common\model\Member;
use app\common\model\UserBlackRecord;

/**
 * Api基础逻辑
 */
class PlanBase extends LogicBase
{

    /**
     * API返回数据
     */
    public function apiReturn($code_data = [], $return_data = [], $return_type = 'json')
    {

        if (is_array($code_data) && array_key_exists(API_CODE_NAME, $code_data)) {
            
            !empty($return_data) && $code_data['data'] = $return_data;

            $result = $code_data;

        } else {
            
            $result = CodeBase::$success;
            
            $result['data'] = $code_data;
        }

        $return_result = $this->checkDataSign($result);

        $return_result['exe_time'] = debug('api_begin', 'api_end');
        
        return $return_type == 'json' ? json($return_result) : $return_result;
    }

    /**
     * 检查是否需要响应数据签名
     */
    public function checkDataSign($data)
    {
        
        $info = $this->modelApi->getInfo(['api_url' => URL]);
        $info['is_response_sign'] && !empty($data['data']) && $data['data']['data_sign'] = create_sign_filter($data['data']);

        return $data;
    }
    
    /**
     * API错误终止程序
     */
    public function apiError($code_data = [])
    {
        
        return throw_response_exception($code_data);
    }

    /**
     * API提交附加参数检查
     */
    public function checkParam($param = [])
    {
        
        $info = $this->modelApi->getInfo(['api_url' => URL]);
        
        empty($info) && $this->apiError(CodeBase::$apiUrlError);
//        if($info['request_data']){
//            foreach ($info['request_data'] as $k=>$v){
//                if($v['is_require']==1){
//                    if(!isset($param[$v['field_name']])){
//                        $this->apiError(ajaxreturn(1,$v['field_name'].'参数必须传'));
//                    }
//                }
//            }
//        }

        (empty($param['access_token']) || $param['access_token'] != '5b7f60aca7e7f6f8c680b1b219ad3ec6') && $this->apiError(CodeBase::$accessTokenError);

        
        $info['is_request_sign']    && (empty($param['data_sign'])      || create_sign_filter($param) != $param['data_sign']) && $this->apiError(CodeBase::$dataSignError);
    }


    /***
     * 验证手机验证吗
     */
    protected  function check_mobile_code($data){

        if(getConfig('sms_is_test')){
            //测试开关
            if($data['code']=='123456') {
               return true;
            }
        }
        if(empty($data['token'])){
            return false;
        }
        $user_sms_bean = $this->modelUserSms->where(array('token' => $data['token'],'area'=>$data['area'], 'mobile' => $data['mobile'],'code'=>$data['code'], 'status' => 0))->find();
        if (empty($user_sms_bean)) {
            return false;

        }
        return true;
    }


    /***
     * 验证支付密码
     */
    protected  function  check_password($password){
        if(!$this->logicApiBase->member->check_pin_password($password)){
            $userinfo=cache($this->logicApiBase->member['username'].$this->logicApiBase->member['id'].'pinpassword');

            if($userinfo){

                if($userinfo['error_count'] >=2 ){
                    UserBlackRecord::create([
                        'user_id'=>$this->logicApiBase->member['id'],
                        'create_time'=>TIME_NOW,
                        'type'=>3,
                        'remark'=>'多次输入二级密码错误被禁止登录'
                    ]);
                    $this->logicApiBase->member->is_login=0;
                    $this->logicApiBase->member->no_login_time=time()+getConfig('login_time')*3600;
                    $this->logicApiBase->member->save();
                    return  2;

                }else{
                    $userinfo['error_count'] +=1;
                    cache($this->logicApiBase->member['username'].$this->logicApiBase->member['id'].'pinpassword',$userinfo,86400);
                    return  1;
                }
            }else{
                $userinfo=['error_count'=>1,'create_time'=>time()];
                cache($this->logicApiBase->member['username'].$this->logicApiBase->member['id'].'pinpassword',$userinfo,86400);
            }

            return 3;

        }
        cache($this->logicApiBase->member['username'].$this->logicApiBase->member['id'].'pinpassword',null);
        return 4;

    }

    protected function  degui(){
        $code=create_invite_code();
        if(Member::where(['invite_code'=>$code])->find()){
            $this->degui();
        }
        return $code;
    }

}
